import Timers
import GameType
import Player
import Board
import AlgorithmOne
import AlgorithmTwo
import ManualPlayerMove
import Move
import GamePhysics

TIME_PER_MOVE = 10.01
timer = Timers.MyTimer()
actualPlayer= Player.WHITE
gameType = GameType.NONE

def init():
	global actualPlayer
	global gameType
	actualPlayer=Player.WHITE
	Board.resetBoard()
	gameType = selectTypeOfGame()

def selectTypeOfGame():
	print "\n\n\n*********SETTINGS*********\n\n1 - PvP\n2 - PvC (P=white)\n3 - CvP (P=black)\n4 - CvC"
	while True:
		try:
			input = int(raw_input('select: '))
			if input>GameType.NONE and input<GameType.MAX:
				return input
		except ValueError:
			print "Try again"

def getMove(avalaibleMoves, board):
	indexOfMove = 0
	if actualPlayer==Player.WHITE:
		if gameType==GameType.PvP or gameType==GameType.PvC :
			indexOfMove = ManualPlayerMove.getMove(Move.getCopyOfMoves(avalaibleMoves),Board.getCopyOfBoard(board), timer)
		else:
			indexOfMove = AlgorithmOne.getMove(Move.getCopyOfMoves(avalaibleMoves), Board.getCopyOfBoard(board), timer, actualPlayer)
	else:
		if gameType==GameType.PvP or gameType==GameType.CvP:
			indexOfMove = ManualPlayerMove.getMove(Move.getCopyOfMoves(avalaibleMoves),Board.getCopyOfBoard(board), timer)
		else:
			indexOfMove = AlgorithmTwo.getMove(Move.getCopyOfMoves(avalaibleMoves), Board.getCopyOfBoard(board), timer, actualPlayer)
	return avalaibleMoves[indexOfMove]
	
def startGame() :
	global actualPlayer
	init()
	Board.drawBoard()
	while True:
		avalaibleMoves = GamePhysics.getAvalaibleMoves(Board.board,actualPlayer)
		if len(avalaibleMoves)==0:
			GamePhysics.oponentWin(actualPlayer)
			break
		timer.start()
		move = getMove(avalaibleMoves,Board.board)
		print "time of move: "+str(timer.stop())
		if gameType==GameType.CvC and timer.getTime()>TIME_PER_MOVE:	#time for move only for game CvC
			oponentWin(actualPlayer)
			break
		timer.reset()
		GamePhysics.doMove(move,Board.board)
		Board.drawBoard()
		actualPlayer=GamePhysics.changePlayer(actualPlayer)
	raw_input('press enter')

startGame()	
